/*
 * Created on 02-Oct-2008
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package us.esme.api;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Properties;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.ProxyClient;
import org.apache.commons.httpclient.ProxyHost;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * @author I056593
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class EsmeRestApi {

	private String apiUrl;
	private String authToken;
	private HttpClient client = new HttpClient();
	//private HostConfiguration hostConfig = new HostConfiguration();

	public void setProxy(String proxyHost, int proxyPort) {
		//hostConfig.setProxy(proxyHost, proxyPort);
		client.getHostConfiguration().setProxy(proxyHost, proxyPort);
	}

	public EsmeRestApi(String apiUrl) throws IOException {
		this.apiUrl = apiUrl;
	}

	public void sendMsg(String message) {
		Message msg = new Message();
		msg.setText(message);
		sendMsg(msg);
	}

	public void login(String token)
	{
//		POST /api/login
//		token=API_TOKEN
//
//		<esme_api operation="login" success="true"/>

		PostMethod method = new PostMethod(apiUrl + "/login");
		NameValuePair[] data = { new NameValuePair("token", token) };
		try {
			method.setRequestBody(data);
			int statusCode = client.executeMethod(method);
			if (statusCode != 200)
			{
				System.out.println("login response code: "+statusCode);
				throw new EsmeException(statusCode);
			}
			byte[] responseBody = method.getResponseBody();
		} catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			// Release the connection.
			method.releaseConnection();
		}
	}
	
	public void logout()
	{
//		GET /api/logout
//
//		<esme_api operation="logout" success="true"/>

		GetMethod method = new GetMethod(apiUrl + "/logout");
		try {
			int statusCode = client.executeMethod(method);
			if (statusCode != 200)
			{
				System.out.println("logout response code: "+statusCode);
				throw new EsmeException(statusCode);
			}
			byte[] responseBody = method.getResponseBody();
		} catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			// Release the connection.
			method.releaseConnection();
		}
	}
	
	public void sendMsg(Message message) {
//			POST /api/send_msg
//			message=messagebody
//			via=optional_name_of_client
//			tags=optional_comma_delimitted_tags
//			metadata=optional_XML_Element_data
//			replyto=optional_id_of_message

// 			<esme_api operation="send_msg" success="true"/>

		PostMethod method = new PostMethod(apiUrl + "/send_msg");
		String tags="";
		for (int i = 0; i < message.getTags().length; i++) {
			if (i != 0)	tags+=(",");
			tags+=(message.getTags()[i]);
		}
		NameValuePair[] data = { 
			new NameValuePair("message", message.getText()),
			new NameValuePair("via", message.getVia()),
			new NameValuePair("tags", tags),
		};
		try {
			method.setRequestBody(data);
			int statusCode = client.executeMethod(method);
			if (statusCode != 200)
			{
				System.out.println("send_msg response code: "+statusCode);
				throw new EsmeException(statusCode);
			}
			byte[] responseBody = method.getResponseBody();
		} catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			// Release the connection.
			method.releaseConnection();
		}
	}

	/**
	 * @return
	 */
	public String getApiUrl() {
		return apiUrl;
	}

	/**
	 * @param string
	 */
	public void setApiUrl(String string) {
		apiUrl = string;
	}

	/**
	 * @return
	 */
	public String getAuthToken() {
		return authToken;
	}

	/**
	 * @param string
	 */
	public void setAuthToken(String string) {
		authToken = string;
	}

}

